package com.example.commen.base.dialoglib;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.widget.NestedScrollView;

import com.example.commen.R;
import com.example.commen.utile.CommonUtils;


/**
 * 进度dialog,仿照IOS样式
 */
public class XieYiDialog extends Dialog {


    private CommonInterface.PosAndNegtiveListener onClickListener;
    private Context context;
    private LinearLayout ll_xieyi_context;
    private TextView tv_xieyi_dialog,tv_userys_dialog;
    private TextView tv_xieyi_yes,tv_xieyi_no;
    private NestedScrollView ll_xieyi_context_end;

    private XieYiDialog(Context context, int theme) {
        super(context, theme);
    }

    public XieYiDialog(Context context) {
        this(context, null);
    }

    /**
     *
     * @param context
     * @param onClickListener 协议跳转
     */
    public XieYiDialog(Context context, CommonInterface.PosAndNegtiveListener onClickListener) {
        this(context, R.style.Dialog_NoTitleAndBackgrounds);
        this.context = context;
        this.onClickListener = onClickListener;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_xieyi_dialog);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, CommonUtils.getScreenHeight());
        setCanceledOnTouchOutside(false); // 点击屏幕Dialog以外的地方是否消失
        initView();
    }

    private void initView() {
        ll_xieyi_context = findViewById(R.id.ll_xieyi_context);
        tv_xieyi_dialog = findViewById(R.id.tv_userxieyi_dialog);
        tv_userys_dialog = findViewById(R.id.tv_userys_dialog);
        ll_xieyi_context_end = findViewById(R.id.ll_xieyi_context_end);
        tv_xieyi_yes = findViewById(R.id.tv_xieyi_yes);
        tv_xieyi_no = findViewById(R.id.tv_xieyi_no);
        setCancelable(false);
        tv_xieyi_dialog.setText(getLineForText(tv_xieyi_dialog.getText().toString()));
        tv_userys_dialog.setText(getLineForText(tv_userys_dialog.getText().toString()));
        tv_xieyi_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commUtiles.saveBooleanSharedPreferences(context,"config", "isReadXY", true);
                dismiss();
            }
        });

        tv_xieyi_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_xieyi_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener!=null) {
                    onClickListener.onPosittive();
                }
            }
        });
        tv_userys_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onClickListener!=null) {
                    onClickListener.onNegative();
                }
            }
        });
    }

    @Override
    public void show() {
        boolean isReadXY = commUtiles.getBooleanSharedPreferences(context,"config", "isReadXY", false);
        if (!isReadXY) {
            super.show();
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
//            ViewGroup.LayoutParams par = ll_xieyi_context_end.getLayoutParams();
//            if(ll_xieyi_context_end.getHeight()>CommonUtils.getScreenHeight()/3) {
//                par.height = CommonUtils.getScreenHeight() / 3;
//                ll_xieyi_context_end.setLayoutParams(par);
//            }
        }
    }
    public Spanned getLineForText(String str) {
        return Html.fromHtml("<u>" + str + "<u/>");
    }
}
